var ACTION = require("../../base/action");
var utils = require("../../utils/utils");
var time = require("../../utils/time");
var ocrGml = require("../../utils/ocrGml");

function getActionEat(config, ad, contentType) {
  var actionEat = new ACTION(contentType, config);

  actionEat.goStart = function () {
    var ocrItem = ocrGml.captureExistOne("吃饭补贴", true);
    if (ocrItem) {
      this.execFCount++;
      this.updateStorage();
      utils.clickOcrItem(ocrItem);
      sleep(utils.getRandom(2.5, 3, 1000));
    }
    return ocrItem;
  };

  actionEat.beforeExec = function () {
    var content = "领取" + contentType + "补贴";
    if (utils.clickTextStartsWith(content)) {
      this.execSCount++;
      this.updateStorage();
      return true;
    }
    var content = "直接领取";
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      utils.clickUIText(UIText);
      this.execSCount++;
      this.updateStorage();
      return true;
    }
    var content = "再过";
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      this.execSCount++;
      this.updateStorage();
      this.afterExec();
    }
    return false;
  };
  actionEat.exec = function () {
    sleep(utils.getRandom(2, 3, 1000));
    var contents = [
      //prettier-ignore
      "看视频再",
      "看广告再",
    ];
    var UIText = utils.existsOneTextStartsWith(contents);
    if (UIText) {
      utils.clickUIText(UIText);
      sleep(utils.getRandom(2, 3, 1000));
      var contents = ["开心收下", "立即收下"];
      var ocrItem = ocrGml.captureExistOne(contents, true);
      if (ocrItem) {
        utils.clickOcrItem(ocrItem);
        sleep(utils.getRandom(2, 3, 1000));
        return;
      }
      ad.init(1, function () {
        var ocrItem = ocrGml.captureExistOne([
          "时间到啦",
          "开饭时间",
          "每餐可获得",
        ]);
        return ocrItem;
      });
      return;
    }
    var contents = [
      //prettier-ignore
      "开心收下",
      "立即收下",
    ];
    var UIText = utils.existsOneTextStartsWith(contents);
    if (UIText) {
      utils.clickUIText(UIText);
      return;
    }
  };
  actionEat.afterExec = function () {
    console.log(this.name, time.str(), "afterExec");
    sleep(utils.getRandom(2, 3, 1000));
    back();
  };

  return actionEat;
}

module.exports = getActionEat;
